﻿using Minecraft_Armor_Stand_Creater.Util;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;

namespace Minecraft_Armor_Stand_Creater.Resource.Pages
{
    /// <summary>
    /// OptionPage.xaml 的交互逻辑
    /// </summary>
    public partial class OptionPage : Page
    {
        public OptionPage()
        {
            InitializeComponent();

            #region Events
            Loaded += Page_Loaded;
            Lang_CN.Click += LangSave;
            Lang_EN.Click += LangSave;
            Back.Click += Back_Click;

            Theme_Blue.Click += ThemeSave;
            Theme_Green.Click += ThemeSave;
            Theme_Purple.Click += ThemeSave;

            OptionGrid.MouseDown += OptionSave;
            OptionGrid.MouseLeave += OptionSave;
            #endregion
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //设置选项加载
            Lang_CN.IsChecked = ProgramConfig.Instance._langCN;//语言
            Guidance_on.IsChecked = ProgramConfig.Instance.Guidance;//引导
            foreach (var item in Theme_Panel.Children)//主题色
            {
                if (item is RadioButton button && button.Name == ProgramConfig.Instance.DefaultTheme)
                    button.IsChecked = true;
            }
        }

        /// <summary>
        /// 保存语言配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void LangSave(object sender, RoutedEventArgs e)
        {
            ProgramConfig.Instance._langCN = (bool)Lang_CN.IsChecked;//语言
            //语言文件初始化
            ResourceDictionary dict = new ResourceDictionary();
            if (ProgramConfig.Instance._langCN)
                dict.Source = new Uri(@"lang\CN.xaml", UriKind.Relative);
            else
                dict.Source = new Uri(@"lang\EN.xaml", UriKind.Relative);
            Application.Current.Resources.MergedDictionaries[0] = dict;//资源赋值

            ConfigOperator.IniWrite("System", "Lang", ProgramConfig.Instance._langCN.ToString());
        }

        /// <summary>
        /// 保存主题配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ThemeSave(object sender, RoutedEventArgs e)
        {
            ResourceDictionary ThemeDict = new ResourceDictionary();
            ThemeDict.Source = new Uri(((RadioButton)sender).Tag.ToString(), UriKind.Relative);

            Application.Current.Resources.MergedDictionaries[1] = ThemeDict;//资源赋值

            ProgramConfig.Instance.DefaultTheme = ((RadioButton)sender).Name;
            ConfigOperator.IniWrite("System", "Theme", ProgramConfig.Instance.DefaultTheme);
        }

        void OptionSave(object sender, MouseEventArgs e) => GuidanceSave();
        void OptionSave(object sender, MouseButtonEventArgs e) => GuidanceSave();
        /// <summary>
        /// 保存引导配置
        /// </summary>
        void GuidanceSave()
        {
            ProgramConfig.Instance.Guidance = (bool)Guidance_on.IsChecked;//引导开关
            ConfigOperator.IniWrite("System", "Guidance", ProgramConfig.Instance.Guidance.ToString());//引导
        }

        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Back_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}
